$(document).ready(function() {
    $.getJSON('/backend/get-data?data=member-all-names', function(source) {
        $('input[data-provide="typeahead"]').data('source', source)
    })

    $('#analyze').click(function(e) {
        // empty past suggestion
        $('.analysis').hide()
        $('.favorite').hide()

        var member_name = $('input[data-provide="typeahead"]').val()
        var url = '/backend/get-data?data=pred-member'
        $.getJSON(url, {member: member_name}, function(data) {
            column_chart('chart', member_name+' Sales', data.labels, data.series)

            var alert_div = $('.analysis')
            if(data.level < 2)
                alert_div.removeClass('alert-success').addClass('alert-error')
            else if(data.level > 3)
                alert_div.removeClass('alert-error').addClass('alert-success')
            else
                alert_div.removeClass('alert-error alert-success')
            alert_div.find('strong').html('The sale of this member is ' + data.status + '.')
            alert_div.find('span').html(data.suggestion)
            alert_div.fadeIn()

            var favorite = $('.favorite')
            favorite.find('.product').html(data.product)
            favorite.find('.category').html(data.category)
            favorite.fadeIn()
        })
    })
})
